﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ANTProductAssemble_ManageAssemble_TPIMTestadd : System.Web.UI.Page
{
    private TPIMTest tpt;
    private TProductType_PIMInfo tppi;
    private DataRow dr;
    private string productSN
    {
        get 
        {
            return ViewState["ProductSN"].ToString();
        }
        set
        {
            ViewState["ProductSN"] = value;
        }
    }
    private DataTable amddt
    {
        get
        {
            return (DataTable)ViewState["amddt"];
        }
        set
        {
            ViewState["amddt"] = value;
        }
    }


    public ANTProductAssemble_ManageAssemble_TPIMTestadd()
    {
        tpt = new TPIMTest();
        tppi = new TProductType_PIMInfo();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            amddt = new DataTable();
            amddt.Columns.Add(new DataColumn("TestItemName", Type.GetType("System.String")));
            amddt.Columns.Add(new DataColumn("TestValue", Type.GetType("System.Decimal")));
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        productSN = this.TextBox1.Text.Trim();
        IList<TProductType_PIMInfoInfo> list = new List<TProductType_PIMInfoInfo>();
        list = tppi.GetTProductType_PIMInfobySN(productSN);
        if (list.Count < 1)
        {
            string str = "此序列号没有录入过相应的产品类型！请检查";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false); 
        }
        else
        {
            if (amddt.Rows.Count > 0)
            {
                amddt.Rows.Clear();
            }
            foreach (TProductType_PIMInfoInfo item in list)
            {
                dr = amddt.NewRow();
                dr["TestItemName"] = item.TestItemName;
                dr["TestValue"] = 0.00;
                amddt.Rows.Add(dr);
            }
            Binddt();
        }
    }

    private void Binddt()
    {
        this.GridView1.DataSource = amddt;
        this.GridView1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(productSN))
        {
            string str = "请先调出交调测试项";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
        else
        {            
            for (int i = 0; i < amddt.Rows.Count; i++)
            {
                string testValue = ((TextBox)this.GridView1.Rows[i].Cells[1].FindControl("TextBox2")).Text.Trim();
                if (string.IsNullOrEmpty(testValue))
                {
                    int j = i + 1;
                    string str = "第" + j + "项为空，请检查";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                    return;
                }
                else
                {
                    amddt.Rows[i]["TestItemName"] = this.GridView1.Rows[i].Cells[0].Text.Trim();
                    try
                    {
                        amddt.Rows[i]["TestValue"] = Math.Abs(decimal.Parse(testValue));
                    }
                    catch
                    {
                        int j = i + 1;
                        string str = "第" + j + "项不是浮点数据，请检查";
                        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                        return;
                    }
                }
            }
            decimal maxValue = (decimal)amddt.Compute("min(TestValue)", "");
            TPIMTestInfo pti = new TPIMTestInfo(productSN, DateTime.Now, User.Identity.Name, this.DropDownList1.SelectedItem.Text, this.DropDownList2.SelectedItem.Text, maxValue);
            if(tpt.TPIMTestItems_Insert(pti,amddt))
            {
                string str = "交调数据提交成功！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
            else
            {
                string str = "数据提交失败！请联系相关人员";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }
    }
}
